package com.itheima.dao.store;

import com.itheima.domain.store.Catalog;
import com.itheima.domain.store.Company;
import com.itheima.domain.store.Question;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

/**
 * 题库管理  -> 题目管理(数据层)
 */
public interface QuestionDao {
    @Select("select * from st_question order by create_time desc")
    @Results(id = "questionMap", value = {
            @Result(column = "id", property = "id",jdbcType = JdbcType.VARCHAR),
            @Result(column = "company_id", property = "companyId",jdbcType = JdbcType.VARCHAR),
            @Result(column = "catalog_id", property = "catalogId",jdbcType = JdbcType.VARCHAR),
            @Result(column = "remark", property = "remark",jdbcType = JdbcType.VARCHAR),
            @Result(column = "subject", property = "subject",jdbcType = JdbcType.VARCHAR),
            @Result(column = "picture", property = "picture",jdbcType = JdbcType.VARCHAR),
            @Result(column = "analysis", property = "analysis",jdbcType = JdbcType.VARCHAR),
            @Result(column = "type", property = "type",jdbcType = JdbcType.VARCHAR),
            @Result(column = "difficulty", property = "difficulty",jdbcType = JdbcType.VARCHAR),
            @Result(column = "is_classic", property = "isClassic",jdbcType = JdbcType.VARCHAR),
            @Result(column = "state", property = "state",jdbcType = JdbcType.VARCHAR),
            @Result(column = "review_status", property = "reviewStatus",jdbcType = JdbcType.VARCHAR),
            @Result(column = "create_time", property = "createTime",jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "company_id",property = "company",jdbcType = JdbcType.VARCHAR,javaType = Company.class,one = @One(select = "com.itheima.dao.store.CompanyDao.findById")),
            @Result(column = "catalog_id",property = "catalog",jdbcType = JdbcType.VARCHAR,javaType = Catalog.class,one = @One(select = "com.itheima.dao.store.CatalogDao.findById"))
    })
    List<Question> findAll() throws Exception;

    @Select("select * from st_question where id = #{id}")
    @ResultMap("questionMap")
    Question findById(String id) throws Exception;

    @Insert("INSERT INTO st_question(id, company_id, catalog_id, remark, SUBJECT, analysis, TYPE, difficulty, is_classic, state, review_status, create_time ,picture ) VALUES (#{id,jdbcType=VARCHAR}, #{companyId,jdbcType=VARCHAR}, #{catalogId,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{subject,jdbcType=VARCHAR}, #{analysis,jdbcType=VARCHAR}, #{type,jdbcType=VARCHAR}, #{difficulty,jdbcType=VARCHAR}, #{isClassic,jdbcType=VARCHAR}, #{state,jdbcType=VARCHAR}, #{reviewStatus,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{picture,jdbcType=VARCHAR} )")
    int save(Question question) throws Exception;

    @Delete("delete from st_question where id = #{id,jdbcType=VARCHAR}")
    int deleteById(String id)throws Exception;

    @Update("UPDATE st_question SET company_id = #{companyId,jdbcType=VARCHAR}, catalog_id = #{catalogId,jdbcType=VARCHAR}, remark = #{remark,jdbcType=VARCHAR}, SUBJECT = #{subject,jdbcType=VARCHAR}, analysis = #{analysis,jdbcType=VARCHAR}, difficulty = #{difficulty,jdbcType=VARCHAR}, is_classic = #{isClassic,jdbcType=VARCHAR}, picture = #{picture,jdbcType=VARCHAR}, state = #{state,jdbcType=VARCHAR} WHERE id = #{id,jdbcType=VARCHAR}")
    int update(Question question)throws Exception;

    @Select("SELECT * FROM st_question WHERE company_name LIKE #{company}")
    @ResultMap("questionMap")
    List<Question> queryPageByCompany(String company);
}